﻿#ifndef ELEMENTTREEITEM_H
#define ELEMENTTREEITEM_H
#include <QList>
#include <QVariant>
#include <QMap>

class ElementTreeItem
{
    //friend class ElementTreeModel;
public:
    static const int TimeName = Qt::UserRole;
    /* {
        DisplayRole = 0,
        DecorationRole = 1,
        EditRole = 2,
        ToolTipRole = 3,
        StatusTipRole = 4,
        WhatsThisRole = 5,
        // Metadata
        FontRole = 6,
        TextAlignmentRole = 7,
        BackgroundRole = 8,
        ForegroundRole = 9,
        CheckStateRole = 10,
        // Accessibility
        AccessibleTextRole = 11,
        AccessibleDescriptionRole = 12,
        // More general purpose
        SizeHintRole = 13,
        InitialSortOrderRole = 14,
        // Internal UiLib roles. Start worrying when public roles go that high.
        DisplayPropertyRole = 27,
        DecorationPropertyRole = 28,
        ToolTipPropertyRole = 29,
        StatusTipPropertyRole = 30,
        WhatsThisPropertyRole = 31,
        // Reserved
        UserRole = 0x0100
    };*/
    ElementTreeItem();
    ~ElementTreeItem();
    // 返回位于父节点的第几行，顶级节点返回-1
    int rowOnParent() const;
    // 返回指定位置的子节的
    ElementTreeItem* child(int index);
    // 子节点数量
    inline auto childrenCount() const
        {return mChildren.size();}
    // 返回父节点
    inline ElementTreeItem* parent(){return mParent;}
    // 设置父节点为nullptr
    void setParentToNull();
    // 添加子节点
    bool appendChild(ElementTreeItem* child);
    bool insertChild(int index,ElementTreeItem* child);
    // 返回数据
    QVariant value(int dataRole) const;
    // 添加数据
    void setValue(int dataRole,const QVariant&v);
    void setValue(int dataRole,QVariant&&v);
    // 判断是否有
    inline bool have(int dataRole) const
        {return mValues.find(dataRole)!=mValues.end();}
    inline bool valuesCount(){
        return mValues.count();
    }
    inline bool isLeaf(){
        return  mChildren.size()==0;
    }
    inline const QVector<double>& data() const{
        return  mData;
    }
    inline QVector<double>& data(){
        return  mData;
    }
    inline void setData(const QVector<double>& data){
        mData=data;
        mMaxDataIndex = std::max(mData.begin(),mData.end())-mData.begin();
        mMinDataIndex = std::min(mData.begin(),mData.end())-mData.begin();
    }
    void appendData(double data);
    inline double maxData(){
        if(mMaxDataIndex<0||mMaxDataIndex>=mData.count())
            return std::numeric_limits<double>::min();
        return mData[mMaxDataIndex];
    }
    inline double minData(){
        if(mMaxDataIndex<0||mMaxDataIndex>=mData.count())
            return std::numeric_limits<double>::max();
        return mData[mMinDataIndex];
    }
    inline int maxDataIndex(){
        return mMaxDataIndex;
    }
    inline int minDataIndex(){
        return mMinDataIndex;
    }

    // 删除子树
    bool removeChild(ElementTreeItem* child);
    QString name(){
        ElementTreeItem* p=parent();
        if(!p){
            return value(Qt::DisplayRole).toString();
        }
        return p->value(Qt::DisplayRole).toString()+"："+value(Qt::DisplayRole).toString();
    }
private:
    ElementTreeItem* mParent=nullptr;
    QList<ElementTreeItem*> mChildren;
    QMap<int,QVariant> mValues;//rule-value
    QVector<double> mData;
    int mMaxDataIndex = -1;
    int mMinDataIndex = -1;
    // double mMaxData=std::numeric_limits<double>::min();
    // double mMinData=std::numeric_limits<double>::max();
    friend class TreeModeReader;
};

#endif // ELEMENTTREEITEM_H
